package com.tz.config;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * Created by yejf on 2017/1/10.
 * Spring 配置类
 * 通过编程的方式来生成 DataSource Bean, SessionFactory Bean, 以及 PlatformTransactionManager Bean
 */

@Configuration
@ComponentScan({"com.tz.dao","com.tz.service"})
@PropertySource("classpath:db.properties")
@EnableAspectJAutoProxy  //启动AOP
@EnableTransactionManagement //启动基于注解的 声明式事务
public class AppConfig {

    //日志信息
    private static final Logger LOGGER = Logger.getLogger(AppConfig.class);

    public AppConfig() {
        LOGGER.debug("---- 初始化 AppConfig 类型....");
    }

    @Autowired   //通过获取 @PropertySource 注解 指定的属性文件来得到信息
    private Environment env;

    @Bean
    public DataSource getDataSource() {
        LOGGER.debug("--- 创建BasicDataSource 对象.... ");
        BasicDataSource dataSource = new BasicDataSource();
        //给属性赋值
        LOGGER.debug("--- 给数据源的相关属性赋值....");
        dataSource.setDriverClassName(env.getProperty("driver"));
        LOGGER.info("*** 驱动类："+env.getProperty("driver"));
        dataSource.setUrl(env.getProperty("url"));
        LOGGER.info("*** URL："+env.getProperty("url"));
        dataSource.setUsername(env.getProperty("user_name"));
        LOGGER.info("*** 用户名："+env.getProperty("user_name"));
        dataSource.setPassword(env.getProperty("password"));
        LOGGER.info("*** 密码："+env.getProperty("password"));
        //
        String initialSize = env.getProperty("initialSize");
        if(initialSize != null) {
            dataSource.setInitialSize(Integer.parseInt(initialSize));
            LOGGER.debug("--- 设置连接池的初始大小: "+initialSize);
        }
        //返回
        return dataSource;
    }

    @Bean
    @Autowired
    public LocalSessionFactoryBean getSessionFactory(DataSource dataSource) {
        LOGGER.debug("--- 创建LocalSessionFactoryBean 实例...");
        LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
        //设置数据源属性
//        lsfb.setDataSource(this.getDataSource()); //手动调用
        lsfb.setDataSource(dataSource); //通过方法的参数 自动注入
        //设置方言
        Properties props = new Properties();
        props.setProperty("hibernate.dialect",env.getProperty("dialect"));
        props.setProperty("hibernate.show_sql",env.getProperty("show_sql"));
        props.setProperty("hibernate.format_sql",env.getProperty("format_sql"));
        lsfb.setHibernateProperties(props);
        //注册实体类
        lsfb.setPackagesToScan("com.tz.entity");
        //返回
        return lsfb;
    }

    @Bean
    @Autowired //自动注入
    public PlatformTransactionManager getPlatformTransactionManager(SessionFactory sessionFactory) {
        //初始化
        LOGGER.debug("--- 初始化HibernateTranactionManager实例,并以 sessionFactory为参数...");
        HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager(sessionFactory);
        //返回
        return hibernateTransactionManager;
    }
}
